class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        if (n == 0) {
            return -1;
        }
        if (n == 1) {
            return nums[0] == target ? 0 : -1;
        }
        int left = 0;
        int right = n - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            // mid等于target
            if (nums[mid] == target) {
                return mid;
            }
            // left到mid有序,left最小mid最大
            if (nums[0] <= nums[mid]) {
                // 在有序的数组中进行二分查找
                if (target >= nums[0] && nums[mid] > target) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            // mid到right有序，mid最小right最大
            else {
                // 在有序的数组中进行二分查找
                if (target <= nums[n - 1] && nums[mid] < target) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
};